
/*
 * Thi is an android appication which gives both transliteration and translation capabilities
 * Since andriod does not support Sinhala language yet, this application will be great usable for android users
 * This application will very helpful for students who wants to type some sinhala paragraphs in android and 
 * students who needs to search the English meaning for a desired Sinhala word.
 * */
package com.exercise;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FileHandling3Activity extends Activity {
	
	 HashMap<String , String> resultList = new HashMap<String, String>();			// This Hashmap is used to store all data and gives a value when ever we give the key
	 Logic logic = new Logic();
	 
	@Override
    public void onCreate(Bundle savedInstanceState) {								// This method is called at the start of the application
		ArrayList<String> getoutput = new ArrayList();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Data data = new Data();
        final Logic logic = new Logic();
              
        //TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
        //TextView helloTxt1 = (TextView)findViewById(R.id.contents);
        
       // helloTxt.setText(data.name);
        //final EditText textBox1 = (EditText) findViewById(R.id.editText1);
        final AutoCompleteTextView textBox1 = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);		// This is autocomplete text view whic
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, SUGGESIONS);
        textBox1.setAdapter(adapter);
        
        final EditText textBox2 = (EditText) findViewById(R.id.editText2); 
		final Button button1 = (Button) findViewById(R.id.button3);
		//final Button button2 = (Button) findViewById(R.id.button2);
		
		final TextView textArea = (TextView)findViewById(R.id.textArea1);
				
        Typeface typeface = null ;
        typeface = Typeface.createFromAsset(getAssets(), "ISKPOTA.TTF");
        textBox2.setTypeface(typeface);
        textArea.setTypeface(typeface);
        
        resultList = null;
	    resultList = searchEntireData(textArea);                                // This method is used to get all data from a text file and add them to a hashmap
		textArea.setText("Application is ready to use..");			
		
       /* button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//resultList = null;
			    //resultList = searchEntireData(textArea);
				//textArea.setText("Application is ready to use..");
			}
		});*/
 
        button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				logic.unicodeFunction();
		        textBox2.setText(logic.convert(textBox1.getText().toString()));
		        search(textBox2.getText().toString(),textBox1.getText().toString(), resultList,textArea);
			}
		});
        
	}  	
        public void search(String word,String engWord, HashMap<String, String> list,TextView textview) {

        	TextView textvi = textview;
       	 	HashMap<String, String> resultList1 = new HashMap<String, String>();
       	 	resultList1 = list;
       	 	String word1 = word;
       	 	//Object ob = new Object();
       	 	//ob = word;
       	 	
       	 	//Set ref = resultList1.keySet();
       	 	//Iterator it = ref.iterator();
         	//List list1 = new ArrayList();
         	
         	/* while (it.hasNext()) {
         	      Object a = it.next(); 
         	      if(resultList1.get(a).equals(ob)) { 
         	    	  list1.add(a); 
         	      } 
         	 }*/
            
         	 
            String meaning= new String("not found");
        	meaning = resultList1.get(word1);
        	//if()){
        		
        	//}
        	textvi.setText("English Meaning is : "+meaning);      
    }
    
    public  HashMap<String, String> searchEntireData(TextView text1){
    	
    	HashMap<String, String> map = new HashMap<String, String>();
        
        InputStream inputStream = getResources().openRawResource(R.raw.total1);
        Scanner sc=new Scanner(inputStream);
        
        String line;
		
		 while (sc.hasNext()) {
			 line = sc.nextLine();
		        String temp[] = line.split("\\=");
		        String temp2[] = temp[1].split("\\|");
		        
		        for(int x = 0; x < temp2.length;x++){
		            String temp2New = temp2[x].trim();
		           // map.put(temp[0],temp2New);
		            map.put(temp2New, temp[0]);
		        }
		        
		    }        
         return map;
    }
    static final String[] SUGGESIONS = new String[] {
    	"puththarayaa", "iranawa","illanawaa","inna","samaranawaa","sawandenna","sawanak"
    };
}